package com.woniuxy.factory.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.woniuxy.factory.entity.EmpUser;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.EmpUserService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class EmpUserController {
    private String code ="";
    @Autowired
    EmpUserService empUserService;
    //登陆模块
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(EmpUser empUser, HttpServletRequest request,String captcha){
        StringBuilder str = new StringBuilder("");
        if (empUser.getUserName()==null ||empUser.getUserName()==""){
            str.append("账号不能为空");
        }
        if (!captcha.equals(code)){
            str.append("验证码有误！");
        }
        if (empUser.getPassWord()==null ||empUser.getPassWord()==""){
            str.append("密码不能为空");
        }
        if (str.length()!=0){
            return new ResponseResult(AuditType.ALREADY_PASSED,str.toString(),null,null);
        }
        try {
            return empUserService.login(empUser,request);
        }catch (BusinessException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,e.getMessage(),null,null);
        }
    }

    //注册模块
    @ResponseBody
    @PostMapping("/register")
    public ResponseResult register(EmpUser empUser,String passwords,String empId){
        StringBuilder str = new StringBuilder("");
        if (empUser.getUserName()==null ||empUser.getUserName()==""){
            str.append("账号不能为空");
        }
        if (!empUser.getPassWord().equals(passwords)){
            str.append("两次密码不一致");
        }
        if (empUser.getPassWord()==null ||empUser.getPassWord()==""){
            str.append("密码不能为空");
            return new ResponseResult(AuditType.ALREADY_PASSED,str.toString(),null,null);
        }
        try {
            return empUserService.register(empUser,empId);
        }catch (BusinessException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,e.getMessage(),null,null);
        }

    }
    //退出登录
    @PostMapping("/exituser")
    @ResponseBody
    public ResponseResult exitUser(HttpServletRequest request){
        return empUserService.exitUser(request);
    }
    //图形验证码
    @GetMapping("/verification")
    @ResponseBody
    public void verification(HttpServletResponse response){
        //定义图形验证码的长和宽
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        //获取验证码
        code = captcha.getCode();
        //图形验证码写出，可以写出到文件，也可以写出到流
        try {
            captcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
